import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.SimpleDateFormat;

public class Server {
	
	/**
	*
	*/
	
	private static Constant constant = new Constant();
	
	private static double load;
	private static double capacity;
	
	private static String id;
	private static String image;
	private static String privateIP;
	private static String publicIP;
	private static String state;
	private static String key;
	private static String type;
	
	public Server(String id, String image, String privateIP, String publicIP, String state, String key, String type) {
		this.load = 0;
		this.capacity = type.equals("m1.small") ? 1000 : 2000;
		
		this.id = id;
		this.image = image;
		this.privateIP = privateIP;
		this.publicIP = publicIP;
		this.state = state;
		this.key = key;
		this.type = type;
	}
	
	/**
	*	Setter and getter methods for class Server:
	*/
	
	//	Returns the instance ID of the virtual machine associated with this Server instance.
	public static String getInstanceID() {
		return id;
	}
	//	Returns the image used to create the virtual machine associated with this Server instance.
	public static String getImageID() {
		return image;
	}
	//	Returns the private IP address associated with this Server instance.
	public static String getPrivateIP() {
		return privateIP;
	}
	//	Returns the public IP address associated with this Server instance.
	public static String getPublicIP() {
		return publicIP;
	}
	// Returns the current state of this Server instance.
	// Possible return values: pending, running, shutting-down, terminated.
	public static String getState() {
		return state;
	}
	// Returns private key associated with this Server instance.
	public static String getKey() {
		return key;
	}
	// Returns the type of virtual machine associated with this Server instance.
	// Possible return values: m1.small, c1.medium, m1.large, c1.xlarge, c1.xlarge.
	public static String getVMType() {
		return type;
	}
	
	public static double getLoad() {
		return (load / capacity);
	}
	
	public static double getCapacity() {
		return capacity;
	}
	
	public static void updateLoad(double requirement) {
		load += requirement;
	}
	
	public static void updateState() {
		String path = "/var/lib/eucalyptus/LSS/commands/";
		try {
			Process process = Runtime.getRuntime().exec(path + "describe-instance " + id);
			try { 
				process.waitFor();
			}
			catch(Exception e) {
				writeLog(e.getMessage());
			}
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String processResponse;
			int line = 0;
			while((processResponse = bufferedReader.readLine()) != null) {
				if(++line == 2) {
					String instance[] = processResponse.split("	");
					state = instance[constant.state].trim();
				}
			}
		}
		catch(Exception e) {
			writeLog(e.getMessage());
		}
	}
	
	/**
	*	Private methods for class Server:
	*/
	
	private static void writeLog(String msg) {
		String path = "/var/lib/eucalyptus/LSS/logs/";
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMMyyyy");
		String filename = path + "server." + simpleDateFormat.format(calendar.getTime()) + ".log";
		try {
			FileWriter fileWriter = new FileWriter(filename, true);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(msg + "\n");
			bufferedWriter.close();
		}
		catch (Exception e) {
			System.err.println(e);
		}
	}

}